package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.JobDemandMapper;
import com.example.mapper.ResumeClassifyMapper;
import com.example.mapper.ResumeMapper;
import com.example.pojo.JobDemand;
import com.example.pojo.Resume;
import com.example.pojo.ResumeClassify;
import com.example.service.IResumeService;
import com.example.util.EmailUtil;
import com.example.vo.ResultVo;
import com.example.vo.per.ResuSelAllPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import java.util.List;

/**
 * <p>
 * 简历表  服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-04-11
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements IResumeService {

    @Autowired
    private ResumeMapper rm;
    @Autowired
    private ResumeClassifyMapper rcm;
    @Autowired
    private JobDemandMapper jdm;

    @Override
    public ResultVo getResumeByEmpId(Integer empId) {
        Resume resume = baseMapper.selectResumeByEmpId(empId);
        return ResultVo.response(resume);
    }

    @Override
    public PageInfo<Resume> selrsall(ResuSelAllPage rsp) {
        PageHelper.startPage(rsp.getPageNum(), rsp.getPageSize());
        List<Resume> list = rm.selByAll(rsp);
        list.forEach(o->{
        });

        if(!list.isEmpty()){
            return new PageInfo<>(list);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insRes(Resume resume) {
        // 根据身份证判断简历是否存在
        Resume card = rm.selectOne(new QueryWrapper<Resume>().eq("id_card", resume.getIdCard()));
        if (card == null){
            rm.insert(resume);
            return resume.getId();
        }
        // 判断 简历状态 / 是否删除
        if (card.getDeleted() == 1){
            // 复原 简历-简历详情
            rm.update(card,new UpdateWrapper<Resume>().eq("deleted",0));
            ResumeClassify c = new ResumeClassify();
            c.setIsHire(0);
            c.setIsSendOffice(0);
            c.setIsEntry(0);
            rcm.update(c,new UpdateWrapper<ResumeClassify>().eq("resume_id",card.getId()));
        }
        return null;
    }

    @Override
    public ResultVo getResumeWindowData(){
        List<Resume> list = baseMapper.selectResumeWindowData();
        return ResultVo.response(list);
    }

    @Override
    @Synchronized
    public List<Resume> selIds(List<Integer> list) {
        List<Resume> resumes = rm.selectByIdList(list);
        for (Resume resume : resumes) {
            new Thread(() -> {
                try {
                    EmailUtil eu = new EmailUtil();
                    eu.send("3507641746@qq.com"
                            , "简历投递-提醒"
                            , "简历通知 - 提醒"
                            , resume.getEmail());
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }).start();
            UpdateWrapper<Resume> uw = new UpdateWrapper<>();
            uw.eq("id", resume.getId());
            uw.set("status", 0);
            update(uw);
        }
        return resumes;
    }

    @Override
    @Synchronized
    public void rejectO(Integer[] list){
        for (int i = 0; i < list.length; i++) {
            UpdateWrapper<Resume> uw = new UpdateWrapper<>();
            uw.eq("id", list[i]);
            uw.set("status", 2);
            update(uw);
        }
    }

    @Override
    public void delResuById(Integer[] ids) {
        for (Integer id : ids) {
            Resume resume = new Resume();
            resume.setId(id);
            resume.setDeleted(1);
            rm.updateById(resume);
        }
    }
}
